﻿using MediatR;
using RBAC.ApiWrites.Applocation.Command;
using RBAC.Domain.RbacEntity;
using RBAC.Infrasturcture.Interfaces;
using RBAC.ResultCode;

namespace RBAC.ApiWrites.Applocation.Commandhandler
{
    public class DeleteUserCommandhandler : IRequestHandler<DeleteUserCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<User> userrepository;
        private readonly IBaseRepository<UserRole> userrolerepository;

        public DeleteUserCommandhandler(IBaseRepository<User> userrepository, IBaseRepository<UserRole> userrolerepository)
        {
            this.userrepository = userrepository;
            this.userrolerepository = userrolerepository;
        }
        public async Task<ApiResult<int>> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            var userId = userrepository.GetAll().FirstOrDefault(x => x.UserId == request.UserId);

            var res = await userrepository.Delete(userId);
            var Rolelist = userrolerepository.GetAll().Where(x => x.UserId == request.UserId).ToList();
            await userrolerepository.DeleteRange(Rolelist);






            return new ApiResult<int>()
            {
                Code = ApiResultEnum.删除成功
                ,
                Message = ApiResultEnum.删除成功.ToString(),
                Data = res
            };
        }
    }
}
